//index.js
//获取应用实例
const app = getApp()
const getDateDiff = require('../../utils/time.js').getDateDiff
const formatTime = require('../../utils/time.js').formatTime

Page({
  data: {
    eventList: null,
    timeDiff: [],

  },

  bindEventDetail(e) {


    const index = e.currentTarget.dataset.index
    const event = wx.getStorageSync('eventList')[index]
    const event_json = JSON.stringify(event)

    // 将事件对象传过去，可直接展示
    wx.navigateTo({
        url: './event_detail/event_detail?event_json=' + event_json
    })
  },

  addEvent() {
    //console.log("jjj")
    wx.navigateTo({
      url: './add_event/add_event',
    })
  },
  
  onLoad: function () {
  },
  onShow: function () {
    this.setData({
      eventList: wx.getStorageSync('eventList')
    })

    for (let i = 0; i < this.data.eventList.length; i++) {

      //当前时间 - 事件时间, 结果为正整数则事件已经过去，负整数则表示事件还没到
      let time = getDateDiff(this.data.eventList[i].date, formatTime(new Date()))
      


      let info = ''
      if (time > 0 ) {
        info = '已经 ' + time + ' 天'
      } else if (time < 0) {
        info = '还有 ' + -time + ' 天'
      } else {
        info = '就在今天'
      }

      const timeDiff = this.data.timeDiff
      timeDiff.push(info)
      this.setData({
        timeDiff: timeDiff
      })


    } 


  }
})
